package ru.simplemaps.osmapi;

import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.io.InputStream;

/**
 * Class parses OSM data
 *
 * @author enaku_adm
 * @since 22.07.2010 11:23:13
 */
public class OSMParser {
    public static OSMData parse(InputStream in) throws SAXException, ParserConfigurationException, IOException {
        //todo do not create new instances. reuse
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        OSMContentHandler osmContentHandler = new OSMContentHandler();
        reader.setContentHandler(osmContentHandler);
        reader.parse(new InputSource(in));
        return osmContentHandler.getData();
    }

    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
        long start = System.currentTimeMillis();

        DefaultHttpClient client = new DefaultHttpClient();
//        OSMData data = new GetHighwaysMethod(client).execute();

        System.out.println("time = " + (System.currentTimeMillis() - start));
//        System.out.println("nodes.size = " + data.nodes.size());
//        System.out.println("ways.size = " + data.ways.size());
//        System.out.println("relations.size = " + data.relations.size());
    }

//        return "http://api.openstreetmap.org/api/0.6/map?bbox=37.3967743,55.7062241,37.796402,55.855275";
//        return "http://api.openstreetmap.org/api/0.6/map?bbox=37.6648442,55.7769297,37.6795834,55.791501899999995";
//        return "http://www.informationfreeway.org/api/0.6/way%5Bhighway=*%5D%5Bbbox=37.6648442,55.7769297,37.6695834,55.781501899999995%5D";
//        return "http://xapi.openstreetmap.org/api/0.6/way%5Bhighway=*%5D%5Bbbox=37.6648442,55.7769297,37.6695834,55.781501899999995%5D";
}
